<?php

namespace App\Console\Commands\Common;

use App\Trip\Translate\Niutrans\Translation;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;

class WeatherLanguage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'weather_lang:generate {--language=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成天气语言包';
    /**
     * @var string[]
     */
    private array $base_key;
    /**
     * @var string[]
     */
    private array $langs;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->langs    = [
            'zh',
            'uy',
            'ru',
        ];
        $this->base_key = [
            '晴',
            '少云',
            '晴间多云',
            '多云',
            '阴',
            '有风',
            '平静',
            '微风',
            '和风',
            '清风',
            '强风/劲风',
            '疾风',
            '大风',
            '烈风',
            '风暴',
            '狂爆风',
            '飓风',
            '热带风暴',
            '霾',
            '中度霾',
            '重度霾',
            '严重霾',
            '阵雨',
            '雷阵雨',
            '雷阵雨并伴有冰雹',
            '小雨',
            '中雨',
            '大雨',
            '暴雨',
            '大暴雨',
            '特大暴雨',
            '强阵雨',
            '强雷阵雨',
            '极端降雨',
            '毛毛雨/细雨',
            '雨',
            '小雨-中雨',
            '中雨-大雨',
            '大雨-暴雨',
            '暴雨-大暴雨',
            '大暴雨-特大暴雨',
            '雨雪天气',
            '雨夹雪',
            '阵雨夹雪',
            '冻雨',
            '雪',
            '阵雪',
            '小雪',
            '中雪',
            '大雪',
            '暴雪',
            '小雪-中雪',
            '中雪-大雪',
            '大雪-暴雪',
            '浮尘',
            '扬沙',
            '沙尘暴',
            '强沙尘暴',
            '龙卷风',
            '雾',
            '浓雾',
            '强浓雾',
            '轻雾',
            '大雾',
            '特强浓雾',
            '热',
            '冷',
            '未知',
            '无风向',
            '东北',
            '东',
            '东南',
            '南',
            '西南',
            '西',
            '西北',
            '北',
            '旋转不定',
        ];
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws Exception|GuzzleException
     */
    public function handle()
    {

        // 默认 uy
        $input_language = $this->option('language');
        $language       = 'uy';
        if ($input_language && in_array($input_language, $this->langs)) {
            $language = $input_language;
        }

        $file = lang_path("{$language}/weather.php");
        $data = "<?php\n";
        $data .= "return [\n";
        foreach ($this->base_key as $value) {
            $translation = (new Translation($value))->translate()->getTranslation();
            $data        .= "\t'{$value}' => '{$translation}',\n";
        }
        $data .= "];";
        file_put_contents($file, $data);
        return 0;
    }
}
